package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketAdmin;
import com.cskaoyan.bean.PO.MarketAdminExample;
import com.cskaoyan.bean.PO.MarketStorage;
import com.cskaoyan.bean.PO.MarketStorageExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.utils.MyGetCurrentTime;
import com.cskaoyan.utils.MyPageHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminStorageServiceImpl
 * @Description
 * @Author Lxz
 * @Date 2022-8-13 8:57
 */
@Service
public class AdminStorageServiceImpl implements AdminStorageService{
    @Autowired
    MarketStorageMapper marketStorageMapper;


    @Override
    public CommonData list(String name, String key, BasePageInfo info) {
        MyPageHelper.startPage(info);

        MarketStorageExample example = new MarketStorageExample();
        if (info.getOrder() != null && info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }
        MarketStorageExample.Criteria criteria = example.createCriteria();

        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (key != null && !"".equals(key)) {
            criteria.andNameLike("%" + key + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);

        PageInfo<MarketStorage> pageInfo = new PageInfo<>(marketStorages);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }

    @Override
    public MarketStorage create(String storageKey, String originalFilename, String contentType, long size, String url) {
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(storageKey);
        marketStorage.setName(originalFilename);
        marketStorage.setType(contentType);
        marketStorage.setSize(size);
        marketStorage.setUrl(url);
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketStorage.setAddTime(currentTime);
        marketStorage.setUpdateTime(currentTime);
        marketStorageMapper.insertSelective(marketStorage);
        return marketStorage;
    }

    @Override
    public MarketStorage update(MarketStorage marketStorage) {
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketStorage.setUpdateTime(currentTime);
        marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
        return marketStorage;
    }

    @Override
    public void delete(MarketStorage marketStorage) {
        marketStorage.setDeleted(true);
        Date currentTime = MyGetCurrentTime.getCurrentTime();
        marketStorage.setUpdateTime(currentTime);
        marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
    }
}
